﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinForm1
{
    internal class OutAndInFileInfos
    {
        public static void LoadFileByPath(ref List<PathInfos> fileList, String filePath, bool IsChecked)
        {
            //List<String> noAuthPaths;
            PathInfos pathInfos;
            DirectoryInfo directory = null;
            directory = new DirectoryInfo(filePath);
            if (directory.Exists)
            {
                FileInfo[] fileInfos;
                DirectoryInfo[] directoryInfos;

                fileInfos = directory.GetFiles("*", SearchOption.TopDirectoryOnly);
                directoryInfos = directory.GetDirectories("*", SearchOption.TopDirectoryOnly);
                foreach (FileInfo fileInfo in fileInfos)
                {
                    pathInfos = new PathInfos();
                    pathInfos.FileName = fileInfo.Name;
                    //MessageBox.Show(fileInfo.Name);
                    pathInfos.FilePath = fileInfo.FullName;
                    pathInfos.FileSize = fileInfo.Length;
                    pathInfos.FileType = fileInfo.Extension.Substring(1, fileInfo.Extension.Length - 1);
                    pathInfos.FileDate = fileInfo.CreationTime;
                    fileList.Add(pathInfos);
                }
                foreach (DirectoryInfo directoryInfo in directoryInfos)
                {
                    pathInfos = new PathInfos();
                    pathInfos.FileName = directoryInfo.Name;
                    //MessageBox.Show(directoryInfo.Name);
                    pathInfos.FilePath = directoryInfo.FullName;
                    pathInfos.FileSize = GetDirectorySize(directoryInfo.FullName);
                    pathInfos.FileType = "文件夹";
                    pathInfos.FileDate = directoryInfo.CreationTime;
                    fileList.Add(pathInfos);
                }
                if (IsChecked)
                {

                    try
                    {
                        foreach (DirectoryInfo dirInfo in directoryInfos)
                        {
                            LoadFileByPath(ref fileList, dirInfo.FullName, IsChecked);
                        }
                    }
                    catch
                    {
                        //当前目录没有访问权限
                    }
                }


            }
        }


        public static double GetDirectorySize(String filePath)
        {
            double size = 0;
            try
            {
                DirectoryInfo directory = new DirectoryInfo(filePath);
                FileInfo[] fileInfos = directory.GetFiles("*", SearchOption.TopDirectoryOnly);
                foreach (FileInfo fileInfo in fileInfos)
                {
                    size += fileInfo.Length;
                }
            }
            catch { }
            
            return size;
        }

        public static string CSVExport(List<PathInfos> fileList,String exPath)
        {
            if (exPath.LastIndexOf("\\") != (exPath.Length - 1))
            {
                exPath = exPath + "\\";
            }
            String filePath = exPath + System.DateTime.Now.ToString().Replace(":","-").Replace("/","-") + "CSVResult.txt";
            MessageBox.Show(filePath);
            using (StreamWriter sw = new StreamWriter(filePath, false, Encoding.UTF8))
            {
                foreach (PathInfos fileInfo in fileList)
                {
                    int index = fileInfo.FilePath.LastIndexOf("\\");
                    string line = fileInfo.FileName + ',' +
                                   fileInfo.FilePath.Substring(0, index + 1) + ',' +
                                   fileInfo.FileType + ',' +
                                   Math.Round(fileInfo.FileSize / 1024, 2) + "KB" + ',' +
                                   fileInfo.FileDate; // 
                    sw.WriteLine(line); 
                }
            }
            return filePath;
        }
    }
}
